package pcs.inline.gtalk;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Im;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * A list view example where the data comes from a cursor.
 */
public class ContactListActivity extends ListActivity {
	private static final String LOG_TAG = "ContactListActivity";
	//private static final int CHAT_REQUEST = 10;
	
	private ListAdapter contactsAdapter;
	private Cursor contactsCursor;

	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.gtalk_contacts);		
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Get a cursor with all contacts
		updateContactlist();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		// Gostaria de limpar a lista aqui				
	}

	public void updateContactlist() {
		/*
		 * Status:
		 * 0 - Offline
		 * 2 - Ocupado
		 * 4 - Away
		 * 5 - Avaliable
		 */
		
		/* Consulta SQL *//* Consulta SQL */
		// Colunas a se pegar do BD
		//String[] projection = { Im.Contacts.PRESENCE_STATUS, Im.Contacts.NICKNAME, Im.Contacts.USERNAME};
		
		
		// WHERE - Listar somente os contatos online
		String sqlSelection = Im.Contacts.PRESENCE_STATUS + "!= 0 AND " + Im.Contacts.PRESENCE_STATUS + "!= 1";
		// ORDER BY
		String sqlOrderBy = Im.Contacts.PRESENCE_STATUS + " DESC," + Im.Contacts.NICKNAME + " ASC";
		
		contactsCursor = getContentResolver().query(Im.Contacts.CONTENT_URI,
				null, sqlSelection, null, sqlOrderBy);
		startManagingCursor(contactsCursor);
		contactsCursor.first();
				
		String[] from = {Im.Contacts.PRESENCE_STATUS, Im.Contacts.NICKNAME};
		int[] to = new int[] { R.id.status, R.id.nickname };
		contactsAdapter = new SimpleCursorAdapter(this,
				R.layout.gtalk_contact_row, contactsCursor, from, to) {			
			@Override
			public void setViewImage(ImageView v, String value) {
				super.setViewImage(v, decodeStatusImage(v, value));
			}
		}; 
		
		setListAdapter(contactsAdapter);		
	}
		
	private String decodeStatusImage (ImageView v, String value) {
		if (v.getId() == R.id.status) {
			switch (Integer.valueOf(value)) {
				case Im.Presence.AVAILABLE:
					return String.valueOf(R.drawable.online);
				case Im.Presence.AWAY:
					return String.valueOf(R.drawable.away);
				case Im.Presence.IDLE:
					return String.valueOf(R.drawable.away);
				case Im.Presence.DO_NOT_DISTURB:
					return String.valueOf(R.drawable.busy);
				default:
					return value;				
			}
		}
		else return value;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		contactsCursor.moveTo(position);
		// Pega nome de usuario
		String columnName = Im.Contacts.USERNAME;
		int columnIndex = contactsCursor.getColumnIndex(columnName);
		String contactUsername = contactsCursor.getString(columnIndex);
		
		// Pega status
		columnName = Im.Contacts.PRESENCE_STATUS;
		columnIndex = contactsCursor.getColumnIndex(columnName);
		int status = contactsCursor.getInt(columnIndex);		
		
		Log.i(LOG_TAG, "onListItemClick " + contactUsername);
		try {
			contactsCursor.close();			
		} catch (RuntimeException e) {
			Log.i(LOG_TAG, "onListItemClick " + e.getMessage());
		}
		/* Chama tela de conversa
		Intent chatIntention = new Intent(this, GTalkClient.class);		
		chatIntention.putExtra("username", contactUsername);
		startSubActivity(chatIntention, CHAT_REQUEST);
		*/
		Bundle extras = new Bundle();
		extras.putInt("status", status);
		setResult(RESULT_OK, contactUsername, extras);
		finish();
	}
	
}